

function [f,gr] = mr_no_randomE2(b)

global dat
global dat2
global g
global D
global k

yy=0;
gra=zeros(1,k+0+2);

for i = 1:g
    yt = dat(i).yt;
    wt = dat(i).wt;
    xt = dat(i).xt;
   
    Mg = dat2(i).Mg;
    
    [mr junk] = size(xt);   
     
    
    indp_b1=zeros(mr,1); 
    indp_b2=zeros(mr,1);
    gra_g=zeros(mr,k+0+2);  
        bb=xt*b(1:k+0)+Mg*b(k+0+1)+b(k+0+2)*ones(mr,1); 
          
                       
        indp_b1=indp_b1+ 1./(1+exp(-2*bb));
        indp_b2=indp_b2+ 1./(1+exp(2*bb));
   
     yy=yy+ ((ones(mr,1)+yt)'/2*log(indp_b1)+(ones(mr,1)-yt)'/2*log(indp_b2));
    
  
        temp4=[xt Mg ones(mr,1)];  % remember to include the constant term.    
            
        gra_g=gra_g+temp4;
        
  
    gra1=2*((ones(mr,1)+yt)/2-indp_b1)'*gra_g;


    
    gra=gra+gra1;
end

    f=-yy;
    
    
    gr = -gra';
    